/*
 * Copyright (C) 2018 Jenly Yu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sintohk.qrcoder;

import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;

import com.king.zxing.CameraScan;
import com.king.zxing.CaptureActivity;
import com.king.zxing.util.CodeUtils;
import com.king.zxing.util.LogUtils;
import com.sinothk.qrcoder.R;
import com.sintohk.qrcoder.util.StatusBarUtils;
import com.sintohk.qrcoder.util.UriUtils;

/**
 * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
 */
public class QrCoderActivity extends CaptureActivity {
    public static final int REQUEST_CODE_PHOTO = 0X02;
    boolean needByImage = false;
    private String titleStr = "扫一扫";
    ImageView ivRight;

    @Override
    public int getLayoutId() {
        return R.layout.qr_coder_activity;
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Toolbar toolbar = findViewById(R.id.toolbar);
        StatusBarUtils.immersiveStatusBar(this, toolbar, 0.2f);
        TextView tvTitle = findViewById(R.id.tvTitle);

        String title = getIntent().getStringExtra("title");
        if (title != null && title.length() > 0) {
            titleStr = title;
        }
        tvTitle.setText(titleStr);

        needByImage = getIntent().getBooleanExtra("needByImage", false);
        if (needByImage) {
            ivRight = this.findViewById(R.id.ivRight);
            ivRight.setImageResource(R.drawable.qr_coder_img);
            ivRight.setClickable(true);
            ivRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    startActivityForResult(pickIntent, REQUEST_CODE_PHOTO);
                }
            });
        }
    }

    @Override
    public void initCameraScan() {
        super.initCameraScan();
        getCameraScan()
                .setPlayBeep(true)
                .setVibrate(true);
    }

    public void onClick(View v) {
        if (v.getId() == R.id.ivLeft) {
            onBackPressed();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            switch (requestCode) {
                case REQUEST_CODE_PHOTO:
                    parsePhoto(data);
                    break;
            }

        }
    }

    private void parsePhoto(Intent data) {
        final String path = UriUtils.getImagePath(this, data);
        LogUtils.d("path:" + path);
        if (TextUtils.isEmpty(path)) {
            return;
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                final String result = CodeUtils.parseCode(path);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = getIntent();
                        intent.putExtra(CameraScan.SCAN_RESULT, result);
                        setResult(RESULT_OK, intent);
                        finish();
//                        Toast.makeText(QrCoderActivity.this, result, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }).start();
    }
}
